package plus.easydo.easypush.handler;

import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import plus.easydo.easypush.oauth.AuthStateRedisCache;


/**
 * 自定义权限实现
 *
 * @author yuzhanfeng
 */
@Aspect
@Component
public class CustomizePreAuthorizeAspect {

private static final String CHECK_PARAMETER_NAME = "token";

    @Autowired
    AuthStateRedisCache authStateRedisCache;

    /**
     * @param point  切点
     * @return 结果
     * @throws Throwable HasPermissionException
     */
    @Around("@annotation(plus.easydo.easypush.annotation.AuthCheck)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Object[] args = point.getArgs();
        String[] parameterNames = methodSignature.getParameterNames();
        int index = ArrayUtils.indexOf(parameterNames, CHECK_PARAMETER_NAME);
        if (index != -1) {
            authStateRedisCache.checkToken((String) args[index]);
        }
        return point.proceed();
    }


}
